package com.itheima.aspect;


import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.OperateLog;
import com.itheima.pojo.Result;
import com.itheima.utils.CurrentHolder;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Slf4j
@Component
@Aspect
public class OperationLogAspect {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Autowired
    private HttpServletRequest request;

    @Around("execution(* com.itheima.service.*Service.*(..)) && @annotation(com.itheima.anno.LogOperation)")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {

        //开始时间
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long end = System.currentTimeMillis();
        long time = end - start;

        //1.定义操作日志对象，封装数据
        OperateLog operateLog = new OperateLog();
        operateLog.setOperateEmpId(getId());//员工id
        operateLog.setOperateTime(LocalDateTime.now());//操作时间
        operateLog.setClassName(joinPoint.getTarget().getClass().getName());//类名
        operateLog.setMethodName(joinPoint.getSignature().getName());//方法名
        operateLog.setMethodParams(Arrays.toString(joinPoint.getArgs()));//方法参数
        operateLog.setReturnValue(proceed.toString());//返回值
        operateLog.setCostTime(time);//方法执行时长

        //2.调用持久化操作日志
        operateLogMapper.insert(operateLog);
        return proceed;
    }
    public Integer getId(){
        return CurrentHolder.getCurrentId();
    }
}
